package com.boot.learning.mockito.mongo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boot.learning.exception.NotFoundException;
import com.boot.learning.powermock.RandomStrUtils;

/**
 * 用户详情实现 （Mongo）
 * @version 1.0
 * @author fuhw
 */
@Service
public class UserDetailServiceImlp implements UserDetailService {

	@Autowired
	private UserDetailRepository repository;
	
	@Override
	public List<UserDetail> findAll(){
		return repository.findAll();
	}

	@Override
	public UserDetail addUser(UserDetail userDetail) {
		return repository.save(userDetail);
	}
	
	

	@Override
	public UserDetail updateUsername(String username) {
		UserDetail findOne = repository.findOne(username);
		if(findOne == null) {
			throw new NotFoundException("没有找到该用户");
		}
		String randomPassword = RandomStrUtils.getRandomStr(6);
		findOne.setPassword(randomPassword);
		UserDetail saveResult = repository.save(findOne);
		return saveResult;
	}
	
}
